package com.eliza.base.views.device.info

import android.app.Activity
import android.app.admin.DeviceAdminReceiver
import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.telephony.TelephonyManager
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavController


@Composable

fun Wifi() {
    Text(text = "DeviceInfo")
    val current = LocalContext.current

    val pm: PackageManager = current.packageManager
}


@Composable
fun DeviceInfoScreen(it: NavController) {
    Text(text = "DeviceInfo")
    val current = LocalContext.current
    val appInfo = AppInfo()
    val hardWare = HardWare()
    val pm: PackageManager = current.packageManager

    hardWare.device = Build.DEVICE;
    hardWare.model = Build.MODEL;
    hardWare.board = Build.BOARD;
    hardWare.brand = Build.BRAND;
    hardWare.manufacturer = Build.MANUFACTURER;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
        hardWare.deviceId = current.deviceId
    } else {
        val telephonyManager =
            current.getSystemService(Context.TELEPHONY_SERVICE) as (TelephonyManager)
        val devicePolicyManager =
            current.getSystemService(Context.DEVICE_POLICY_SERVICE) as (DevicePolicyManager)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
            devicePolicyManager.isDeviceFinanced
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            devicePolicyManager.wifiSsidPolicy
        }
    }


    LazyColumn {
        initAppInfo(appInfo, current)
    }
}

private fun initAppInfo(
    appInfo: AppInfo,
    current: Context
) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        appInfo.opPackageName = current.opPackageName
        appInfo.isDeviceUpgrading = current.packageManager.isDeviceUpgrading
        appInfo.isInstantApp = current.packageManager.isInstantApp
        appInfo.isPackageSuspended = current.packageManager.isPackageSuspended
        appInfo.isSafeMode = current.packageManager.isSafeMode
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        appInfo.isAutoRevokeWhitelisted = current.packageManager.isAutoRevokeWhitelisted
    }
    appInfo.packageName = current.packageName
    appInfo.applicationInfo = current.applicationInfo
    appInfo.packageCodePath = current.packageCodePath
    appInfo.isDeviceProtectedStorage = current.isDeviceProtectedStorage
    appInfo.isRestricted = current.isRestricted
}

class DeviceTools(val context: Activity) {
    private val requestCode = 1
    var adminReceiver = ComponentName(context, AdminReceiver::class.java)
    val devicePolicyManager =
        context.getSystemService(Context.DEVICE_POLICY_SERVICE) as (DevicePolicyManager)

    fun requestLockAdmins() {
        //检查是否已经获取设备管理权限
        val active: Boolean = devicePolicyManager.isAdminActive(adminReceiver)
        if (!active) {
            //打开DevicePolicyManager管理器，授权页面
            val intent = Intent()
            //授权页面Action -->  DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN
            intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN)
            //设置DEVICE_ADMIN，告诉系统申请管理者权限的Component/DeviceAdminReceiver
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminReceiver)
            //设置 提示语--可不添加
            intent.putExtra(
                DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                "DevicePolicyManager涉及的管理权限,一次性激活!"
            )
            context.startActivityForResult(intent, requestCode)
        } else {
            Toast.makeText(context, "已经获取的DevicePolicyManager管理器的授权", Toast.LENGTH_LONG)
                .show()
        }
    }

    /**
     * 查看是否已经获得管理者的权限
     * @return resualt
     */
    private fun checkAdmin(): Boolean {
        return devicePolicyManager.isAdminActive(adminReceiver)
    }
}

class AdminReceiver : DeviceAdminReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        super.onReceive(context, intent)
        Log.e("AdminReceiver", "onReceive: ${intent.`package`}")
    }
}